package com.demo.java.OD501_550.OD525;

import java.util.Scanner;
import java.util.*;

/**
 * @author bug菌
 * @Source 公众号：猿圈奇妙屋
 * @des： 【字符串化繁为简(B卷-100分)】问题
 * @url： https://blog.csdn.net/weixin_43970743/article/details/146493056
 */
public class OdMain {
    public static LinkedList<TreeSet<Character>> equal_chars = new LinkedList<>();
    public static boolean flag = false;
    public static String result = "";

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String input_str = in.nextLine(); // 读取输入的字符串

        int k = 0;
        while (k < input_str.length()) {
            if (input_str.charAt(k) == ')') {
                flag = false;
                if (!equal_chars.isEmpty()) {
                    TreeSet<Character> temp = equal_chars.getLast();
                    if (temp.size() == 0) {
                        equal_chars.removeLast();
                    }
                }
            } else if (input_str.charAt(k) == '(') {
                flag = true;
                equal_chars.add(new TreeSet<>());
            } else {
                TreeSet<Character> temp = equal_chars.isEmpty() ? null : equal_chars.getLast();
                if (flag && temp != null) {
                    temp.add(input_str.charAt(k)); // 加入当前括号组
                } else {
                    result += input_str.charAt(k); // 非括号部分直接加入结果
                }
            }
            k += 1;
        }
        global_merge(); // 合并所有的组

        // 处理最终的字符串结果
        char[] tmp_res_arr = result.toCharArray();
        int m = 0;
        while (m < equal_chars.size()) {
            for (int i = 0; i < tmp_res_arr.length; i++) {
                if (equal_chars.get(m).contains(tmp_res_arr[i])) {
                    tmp_res_arr[i] = equal_chars.get(m).first(); // 替换为字典序最小的字符
                }
            }
            m += 1;
        }

        if (tmp_res_arr.length == 0) {
            System.out.println(0);
        } else {
            System.out.println(new String(tmp_res_arr)); // 输出最终结果
        }
    }

    // 合并所有相同的字符组
    public static void global_merge() {
        int i = 0;
        while (i < equal_chars.size()) {
            int j = i + 1;
            while (j < equal_chars.size()) {
                boolean flag = false;
                // 遍历字母a到z
                for (char single_char = 'a'; single_char <= 'z'; single_char++) {
                    char new_char = (char) (single_char - 32);
                    if ((equal_chars.get(i).contains(single_char) || equal_chars.get(i).contains(new_char))) {
                        if ((equal_chars.get(j).contains(single_char) || equal_chars.get(j).contains(new_char))) {
                            flag = true;
                            break;
                        }
                    }
                }
                if (flag) {
                    equal_chars.get(i).addAll(equal_chars.get(j)); // 合并组
                    equal_chars.remove(j); // 删除合并的组
                    global_merge(); // 递归合并
                } else {
                    j += 1;
                }
            }
            i += 1;
        }
    }
}
